﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;//libreria para trabajar con qsl
namespace Web
{
    public partial class Equivalencias : System.Web.UI.Page
    {


        private void LimpiaForm()
        {
            lbl_Producto1.Text = "";
            Txt_Producto_Origen.Text = "";
            Txt_Producto_Pistola.Text= "";
            Cargar_Max_ID();
            Cargar_GridView();

        }

       
        
        private void Cargar_Max_ID()
        {


            lbl_Producto1.Text = BLL.Equivalencias.ObtenerEquivalencias().ToString();
          
       }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                Cargar_Max_ID();
                //Txt_Producto.Enabled = false;
                Cargar_GridView();
            }

           
        }

        private void Cargar_GridView()
        {
            string connstring = ConfigurationManager.ConnectionStrings["connDB"].ToString();
            SqlConnection conn = new SqlConnection(connstring);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = " exec sp_get_Equivalencias";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet dl = new DataSet();
            da.Fill(dl);
            conn.Close();
            GridView_Equivalencias.DataSource = dl;
            GridView_Equivalencias.DataBind();

            cmd.Connection.Close();
            conn.Close();
          
        }

       
        protected void GridView_Equivalencias_SelectedIndexChanged(object sender, EventArgs e)
        {

            
            
            lbl_Producto1.Text = ((Label)GridView_Equivalencias.SelectedRow.FindControl("Label7")).Text;
            Txt_Producto_Origen.Text = ((Label)GridView_Equivalencias.SelectedRow.FindControl("Label8")).Text;
            Txt_Producto_Pistola.Text = ((Label)GridView_Equivalencias.SelectedRow.FindControl("Label9")).Text;
                                          
          //  Cargar_GridView();

        }
        protected void Btn_Aprobar_Click(object sender, EventArgs e)
        {

            string v_OK;

            v_OK = "OK";
            if (Txt_Producto_Origen.Text == "")
            {
                v_OK = "NOK";
            }
            if (Txt_Producto_Pistola.Text == "")
            {
                v_OK = "NOK";
            }

            if (v_OK == "OK")
            {

                string connstring = ConfigurationManager.ConnectionStrings["connDB"].ToString();
                SqlConnection conn = new SqlConnection(connstring);
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "SP_INS_EQUIVALENCIA";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;

                SqlParameter sqlparam1 = new SqlParameter("@EQUIV_ID", SqlDbType.Int, 3);
                SqlParameter sqlparam2 = new SqlParameter("@ID_PRODUCTO1", SqlDbType.VarChar, 20);
                SqlParameter sqlparam3 = new SqlParameter("@ID_PRODUCTO2", SqlDbType.VarChar, 20);
                SqlParameter sqlparam4 = new SqlParameter("@RETORNO", SqlDbType.Int, 1);
                SqlParameter sqlparam5 = new SqlParameter("@MENSAJE", SqlDbType.VarChar, 100);

                sqlparam1.Value = lbl_Producto1.Text;
                sqlparam2.Value = Txt_Producto_Origen.Text;
                sqlparam3.Value = Txt_Producto_Pistola.Text;
                sqlparam4.Value = 0;
                sqlparam5.Value = "";

                cmd.Parameters.Add(sqlparam1);
                cmd.Parameters.Add(sqlparam2);
                cmd.Parameters.Add(sqlparam3);
                cmd.Parameters.Add(sqlparam4);
                cmd.Parameters.Add(sqlparam5);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
               // Cargar_GridView();
                LimpiaForm();
            }
             else
                 Response.Write("<script type=\"text/javascript\">alert('Para Grabar Favor LLenar todos los campos Requeridos..');</script>");
                
            }

            
        
        protected void GridView_Equivalencias_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Eliminar")
            {
                
               // string ID_VAL ;

                int index = Convert.ToInt32(e.CommandArgument);
                //if (((Label)GridView_Equivalencias.SelectedRow.FindControl("Label7")).Text == null)
                //{
             //   ID_VAL = ((Label)GridView_Equivalencias.Rows[index].FindControl("Label7")).Text;
                    
                    
                //}

                string connstring = ConfigurationManager.ConnectionStrings["connDB"].ToString();
                SqlConnection conn = new SqlConnection(connstring);
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "sp_del_equivalencia";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;

                SqlParameter sqlparam1 = new SqlParameter("@EQUIV_ID", SqlDbType.Int, 3);
                SqlParameter sqlparam4 = new SqlParameter("@RETORNO", SqlDbType.Int, 1);
                SqlParameter sqlparam5 = new SqlParameter("@MENSAJE", SqlDbType.VarChar, 100);


                /*
                 lbl_Producto1.Text = ((Label)GridView_Equivalencias.SelectedRow.FindControl("Label7")).Text;
                Txt_Producto_Origen.Text = ((Label)GridView_Equivalencias.SelectedRow.FindControl("Label8")).Text;
                Txt_Producto_Pistola.Text = ((Label)GridView_Equivalencias.SelectedRow.FindControl("Label9")).Text;
                 */


                //sqlparam1.Value = lbl_Producto1.Text;

                sqlparam1.Value = index;
                sqlparam4.Value = 0;
                sqlparam5.Value = "";

                cmd.Parameters.Add(sqlparam1);
                cmd.Parameters.Add(sqlparam4);
                cmd.Parameters.Add(sqlparam5);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                //Cargar_GridView();
                LimpiaForm();

        

                //string connstring = ConfigurationManager.ConnectionStrings["connDB"].ToString();
                //SqlConnection conn = new SqlConnection(connstring);
                //SqlCommand cmd1 = new SqlCommand();

                //cmd1.CommandText = "sp_del_equivalencia";
                //cmd1.CommandType = CommandType.StoredProcedure;
                //cmd1.Connection = conn;

                //SqlParameter sqlparam1 = new SqlParameter("@EQUIV_ID", SqlDbType.SmallInt, 1);
                //SqlParameter sqlparam2 = new SqlParameter("@ID_PRODUCTO1", SqlDbType.VarChar, 20);
                //SqlParameter sqlparam3 = new SqlParameter("@ID_PRODUCTO2", SqlDbType.VarChar, 20);

                //ID_VAL = ((Label)GridView_Equivalencias.SelectedRow.FindControl("Label7")).Text;

                //sqlparam1.Value = ID_VAL;
                //sqlparam2.Value = Txt_Producto_Origen.Text;
                //sqlparam3.Value = Txt_Producto_Pistola.Text;

                //cmd1.Parameters.Add(sqlparam1);
                //cmd1.Parameters.Add(sqlparam2);
                //cmd1.Parameters.Add(sqlparam3);

                //conn.Open();
                //cmd1.ExecuteNonQuery();
                //conn.Close();
                //lbl_Producto1.Text = "";
                //Txt_Producto_Origen.Text = "" ;
                //Txt_Producto_Pistola.Text = "";

                //Cargar_GridView();



            }
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            LimpiaForm();
        }
    }
}
